package scau.osdesign.service;

import javafx.application.Platform;
import scau.osdesign.LaunchMain;
import scau.osdesign.model.Fork;
import scau.osdesign.model.Philosopher;
import scau.osdesign.view.ViewArranger;

import java.util.Vector;

/**
 * Description:本程序实现奇数号哲学家先拿左叉，偶数号哲学家先拿右叉
 */
public class NumberToNumberSim extends Simer {
    public static int PHILOSOPHER_NUMBER = 5;
    public static int FORK_NUMBER = 5;
    public static Vector vector = new Vector<String>();

    public NumberToNumberSim(){}
    @Override
    public void play(Philosopher[] philosophers,Fork[] forks){
        System.out.println("进入到了play()");
        //2.初始化餐叉
        for(int i =0;i<forks.length;i++){
            forks[i] = new Fork();
        }
        //1.初始化哲学家
        for(int i = 0;i<philosophers.length;i++){
            philosophers[i] = new Philosopher(i){
                @Override
                public void run() {
                    while (true) {
                        try {
                            vector.addElement("我是第"+(this.getPhiId()+1)+"号哲学家,我正在思考");
                            this.thinking();
                            while(isPaused){this.waitSelf();}
                            if(this.isInterrupted()){return;}

                            vector.addElement("我是第"+(this.getPhiId()+1)+"号哲学家,我在等待叉子");
                            this.waiting();
                            while(isPaused){this.waitSelf();}
                            if(this.isInterrupted()){return;}

                            if ((this.getPhiId()+1) % 2 == 1) {//奇数号人先拿左叉,偶数号人先拿右叉
                                forks[this.getPhiId()].getLock().acquire();
                                this.getLeftFork(this.getPhiId());
                                while(isPaused){this.waitSelf();}
                                if(this.isInterrupted()){return;}

                                forks[(this.getPhiId() + 1) % FORK_NUMBER].getLock().acquire();
                                this.getRightFork((this.getPhiId() + 1) % FORK_NUMBER);
                                while(isPaused){this.waitSelf();}
                                if(this.isInterrupted()){return;}

                            } else {
                                forks[(this.getPhiId() + 1) % FORK_NUMBER].getLock().acquire();
                                this.getRightFork((this.getPhiId() + 1) % FORK_NUMBER);
                                while(isPaused){this.waitSelf();}
                                if(this.isInterrupted()){return;}

                                forks[this.getPhiId()].getLock().acquire();
                                this.getLeftFork(this.getPhiId());
                                while(isPaused){this.waitSelf();}
                                if(this.isInterrupted()){return;}

                            }
                            vector.addElement("我是第"+(this.getPhiId()+1)+"号哲学家,我得到了左右的叉子，正在就餐");
                            this.eating();
                            while(isPaused){this.waitSelf();}
                            if(this.isInterrupted()){return;}

                            forks[(this.getPhiId() + 1) % FORK_NUMBER].getLock().release();
                            this.releaseRightFork();
                            while(isPaused){this.waitSelf();}
                            if(this.isInterrupted()){return;}

                            forks[this.getPhiId()].getLock().release();
                            this.releaseLeftFork();
                            while(isPaused){this.waitSelf();}
                            if(this.isInterrupted()){return;}

                            vector.addElement("我是第"+(this.getPhiId()+1)+"号哲学家,我吃完了，正在放下叉子");
                            this.finish();
                            if(this.isInterrupted()){return;}

                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
        }
        for(int i = 0;i<philosophers.length;i++){
                philosophers[i].start();
        }
        while(true){
            try{
                while(isPaused){
                    waitSelf();
                }
                if(this.isInterrupted()){
                    Platform.runLater(()->{
                        //null
                    });
                    break;
                }
                Thread.sleep(100);//采样
                Platform.runLater(()->{
                    ViewArranger.updateState(philosophers,forks,vector);
                    vector.clear();
                });
            }catch (InterruptedException e){
                return ;
            }
        }
    }

    @Override
    public void run() {
        play(LaunchMain.philosophers,LaunchMain.forks);
    }
}
